import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/Home.vue'
import store from "@/store"

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: '/home',
    // component: () => import(/* webpackChunkName: "goods" */  '../views/Home.vue')
    component: HomeView,
  },
  {
    path: '/goods',
    name: 'goods',
    component: () => import(/* webpackChunkName: "goods" */  '../views/Goods.vue')
  },
  {
    path: '/user',
    name: 'user',
    component: () => import(/* webpackChunkName: "user" */  '../views/User.vue'),
    redirect: '/user/cart',
    children: [{
      path: 'cart',
      name: 'cart',
      component: () => import(/* webpackChunkName: "user" */  '../components/user/Cart.vue'),
    }, {
      path: 'center',
      name: 'center',
      component: () => import(/* webpackChunkName: "center" */  '../components/user/Center.vue'),
    }

    ]
    // 路由独享守卫
    // beforeEnter: (to, from, next) => {
    //   let token = localStorage.getItem('x-auth-token')
    //   if (token) {
    //     next()
    //   } else {
    //     // alert('请先登录')
    //     store.dispatch('showToast/asyncchangeIsShowToast', {
    //       isShow: true,
    //       msg: "请先登录",
    //       type: "info"
    //     })
    //   }
    //   return;
    // }
  },
  {
    path: '/order',
    name: 'order',
    component: () => import(/* webpackChunkName: "order" */  '../views/Order.vue')
  },
  {
    path: '/free',
    name: 'free',
    component: () => import(/* webpackChunkName: "free" */  '../views/Free.vue')
  },
  {
    path: '/details',
    name: 'details',
    component: () => import(/* webpackChunkName: "details" */  '../views/Details.vue')
  },
  // 404配置
  {
    path: '*',
    name: "error",
    component: () => import(/* webpackChunkName: "error" */  '../components/Error.vue')
  }
]



const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由全局前置守卫
// router.beforeEach((to, from, next) => {
//   if (to.path == '/user') {
//     console.log("打开了user页面");
//     let token = localStorage.getItem('x-auth-token')
//     if (token) {
//       next()
//     } else {
//       // alert('请先登录')
//       store.dispatch('showToast/asyncchangeIsShowToast', {
//         isShow: true,
//         msg: "请先登录",
//         type: "info"
//       })
//     }
//     return;
//   }
//   next()
// })

// 解决路由持续点击报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}


export default router
